package tk.xboot.boot.service;


import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.xboot.boot.dao.domain.mysql.TempTx;
import tk.xboot.boot.dao.mapper.mysql.TempTxMapper;

import javax.annotation.Resource;

/**
 *  需要抛出RuntimeException否则会发生@Transactional不起作用
 *  如不使用try catch 则会抛出 java.lang.ArithmeticException: / by zero
 *  此异常也是一个RuntimeException,由于没有处理，会一直往上抛，到JVM
 *  详细请查看
 * @see Transactional
 *
 * 可知，其默认
 */
@Service
public class MySQLTxService {


    @Resource
    TempTxMapper tempTxMapper;

    /**
     *
     * @param fromUserId
     * @param targetUserId
     * @param count
     */

    @Transactional
    public void transfer(Long fromUserId, Long targetUserId, int count) {
       xx(fromUserId,targetUserId,count);
    }

    @Transactional
    public void xx (Long fromUserId, Long targetUserId, int count){
        try {
            TempTx tt = tempTxMapper.selectByPrimaryKey(fromUserId);
            TempTx tt2 = tempTxMapper.selectByPrimaryKey(targetUserId);

            tt.setBlc(tt.getBlc() - count);
            tt2.setBlc(tt2.getBlc() + count);

            tempTxMapper.updateByPrimaryKey(tt);
            int x = 0;
            int y = 3 / x;
            tempTxMapper.updateByPrimaryKey(tt2);
        } catch (Exception e) {
            throw new RuntimeException();
        }
    }
}
